package com.example.demo.controller;

import com.example.demo.common.OnlyMath;
import com.example.demo.dao.ClassificationTableDao;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.pojo.ClassificationTable;
import com.example.demo.pojo.CommodityList;
import com.example.demo.pojo.PictureSheetOfMerchandise;
import com.example.demo.service.BackgroudGoods;
import com.example.demo.service.GoodsUplond;
import com.example.demo.service.ManageGoods;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@Api(value = "商品上传页",tags = "商品上传页")
@RequestMapping("/goodsUpload")
public class GoodsUpload {
    @Autowired
    GoodsUplond goodsUplond;
    @Autowired
    ClassificationTableDao classificationTableDao;
    @Autowired
    ManageGoods manageGoods;
    @Autowired
    BackgroudGoods backgroudGoods;
    @Autowired
    CommodityListDao commodityListDao;



    @RequestMapping(value = "/goods",method = RequestMethod.POST  )
    @ApiOperation(value = "商品信息上传",notes = "商品信息上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradename",value = "商品名称",dataType = "String"),
            @ApiImplicitParam(name ="price",value = "商品价格",dataType = "double"),@ApiImplicitParam (name="membershipdiscount",value ="商品折扣" ),
            @ApiImplicitParam(name = "commoditydescription",value = "商品说明"),
            @ApiImplicitParam(name = "commodityhighlights",value = "商品亮点"),
            @ApiImplicitParam(name = "specifications",value = "商品规格"),
            @ApiImplicitParam(name = "qualityguaranteeperiod",value = "商品保质期，（精确到天）"),
            @ApiImplicitParam(name = "storagemode",value = "储存方式"),
            @ApiImplicitParam(name = "baozhuang",value = "包装"),
            @ApiImplicitParam(name = "classificationid",value = "二级分类")
   })
    public String addGoods(HttpServletRequest request, String tradename, double price, double membershipdiscount, String commoditydescription, String commodityhighlights, String specifications, String qualityguaranteeperiod, String storagemode , String baozhuang, int classificationid,@ApiParam("file1")MultipartFile file1, @ApiParam("file2")MultipartFile[] file2, @ApiParam("file3")MultipartFile[] file3) throws InterruptedException, IOException {
        System.out.println(classificationid);

        OnlyMath onlyMath=new OnlyMath();
        String SKU=onlyMath.random();
        CommodityList commodityList=new CommodityList();
        commodityList.setBaozhuang(baozhuang);
        commodityList.setSkucode(SKU);
        commodityList.setTradename(tradename);
        commodityList.setPrice(price);
        commodityList.setMembershipdiscount(membershipdiscount);
        commodityList.setCommoditydescription(commoditydescription);
        commodityList.setCommodityhighlights(commodityhighlights);
        commodityList.setSpecifications(specifications);
        commodityList.setQualityguaranteeperiod(qualityguaranteeperiod);
        commodityList.setStoragemode(storagemode);
        commodityList.setBaozhuang(baozhuang);
       commodityList.setClassificationid(classificationid);
        goodsUplond.addGoods(commodityList);
       int id=goodsUplond.findGoodsId(SKU);
        String path="E:\\MeiRiYouXian\\daily_fresh_warehouse\\src\\main\\resources\\templates\\image";
        if(!file1.isEmpty()){
            String name=onlyMath.lastChar(file1.getOriginalFilename());
            System.out.println(name);
            String filename=System.currentTimeMillis() +"."+name;
            File filepath=new File(path,filename);
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            try {
                file1.transferTo(new File(path+File.separator+filename));
                //增加商品主图片路径
                PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
                pictureSheetOfMerchandise.setGoodsid(id);
                pictureSheetOfMerchandise.setIss(1);
                pictureSheetOfMerchandise.setPictureaddress(filename);
                goodsUplond.addGoodsPath(pictureSheetOfMerchandise);
            } catch (IOException e) {
                e.printStackTrace();
            }
            }else {
            return "error";
        }
        for (int i = 0; i < file2.length; i++) {
            String name=onlyMath.lastChar(file1.getOriginalFilename());
            String filename=System.currentTimeMillis() +"."+name;
            file2[i].transferTo(new File(path+File.separator+filename));
            //增加商品其他展示图
            PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
            pictureSheetOfMerchandise.setGoodsid(id);
            pictureSheetOfMerchandise.setIss(2);
            pictureSheetOfMerchandise.setPictureaddress(filename);
            goodsUplond.addGoodsPath(pictureSheetOfMerchandise);
        }
        for (int i = 0; i < file3.length; i++) {
            String name=onlyMath.lastChar(file1.getOriginalFilename());
            String filename=System.currentTimeMillis() +"."+name;
            file3[i].transferTo(new File(path+File.separator+filename));
            //增加商品详情展示图路径
            PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
            pictureSheetOfMerchandise.setGoodsid(id);
            pictureSheetOfMerchandise.setIss(3);
            pictureSheetOfMerchandise.setPictureaddress(filename);
            goodsUplond.addGoodsPath(pictureSheetOfMerchandise);
        }
        return "redirect:/goodsUpload/goodsmessage";
    }



    @ResponseBody
    @RequestMapping(value = "/classification",method=RequestMethod.GET,   produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "商品分类展示" ,tags = "商品分类")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "classificationid",value = "商品分类id"),
//            @ApiImplicitParam(name = "descriptionofspecies",value = "商品种类"),
              @ApiImplicitParam(name = "pid",value = "商品父级分类id"),
//            @ApiImplicitParam(name = "getChildClassify",value = "商品二级分类集合")
    } )
    //商品分类查询
    public List<ClassificationTable> searchGoodsClassification(int pid, Model model){
        System.out.println("ceshi");
        System.out.println(classificationTableDao.findClassficationtableByPid(pid).toString());
        return classificationTableDao.findClassficationtableByPid(pid);
    }



    @RequestMapping(value = "/firstClassification" ,method = RequestMethod.GET)
    @ApiOperation(value = "初次商品分类查询",tags = "初次商品分类查询")
    public String firstClassification(Model model){
        model.addAttribute("map",goodsUplond.firstFindClassfication());
        return "goodsUpload";
    }

   //查找全部商品信息
    @RequestMapping(value = "/goodsmessage" ,method = RequestMethod.GET)
    public String goodsmessage(Model model){
        model.addAttribute("goodss",manageGoods.findAllByCommoditylist());
        return "goods";
    }


    //修改商品信息
    @RequestMapping(value = "/changeGoodsMessage",method = RequestMethod.GET)
    public String changeGoodsMessage(int goodsid ,Model model){
       CommodityList commodityList=backgroudGoods.findAloneGoods(goodsid);
       int a=commodityList.getClassificationid();
       model.addAttribute("first",commodityList);
        ClassificationTable first=backgroudGoods.findFirstClassifybysecond(a);
        List<ClassificationTable>firstList=classificationTableDao.findUnlessfirst(first.getClassificationid());
        firstList.add(0,first);
        ClassificationTable second=backgroudGoods.findClassify(a);
        List<ClassificationTable>secondList=classificationTableDao.findUnlessSecond(second);
        secondList.add(0,second);
       model.addAttribute("firstClassfiy",firstList);
       model.addAttribute("secondClassfiy",secondList);
       return "changeGoods";
    }



    //获取修改的商品信息
    @RequestMapping(value = "/modifyGoodsMessage",method =RequestMethod.GET)
    public String modifyGoodsMessage(CommodityList commodityList , Model mo){
        System.out.println(commodityList.getGoodsid());
        commodityListDao.updateByPrimaryKeySelective(commodityList);
        return "redirect:/goodsUpload/goodsmessage";
    }

}
